﻿using AnQinManagermentBackendAPI.Common.DateBase;
using AnQinManagermentBackendAPI.Common.Request;
using AnQinManagermentBackendAPI.Model.DTO.Department;
using AnQinManagermentBackendAPI.Model.Entity;
using AnQinManagermentBackendAPI.Model.View.Department;
using Microsoft.EntityFrameworkCore;

namespace AnQinManagermentBackendAPI.Service
{
    public class DepartmentService
    {
        /**
         * 数据库
         */
        private readonly DataContext _context;

        /**
         * 构造注入
         */
        public DepartmentService(DataContext context)
        {
            _context = context;
        }

        /**
         * 获取部门列表
         */
        public async Task<RequestResponseModel<PageResult<DepartmentView>>> GetDepartmentList(DepartmentSearchDTO departmentSearchDTO)
        {
            //筛选
            var query = _context.DepartmentView
                .WhereIF(a => a.Code.Contains(departmentSearchDTO.Code), !string.IsNullOrEmpty(departmentSearchDTO.Code))
                .WhereIF(a => a.Name.Contains(departmentSearchDTO.Name), !string.IsNullOrEmpty(departmentSearchDTO.Name))
                ;

            //返回列表
            var departmentList = await query
                .OrderByDescending(a => a.CreateTime)
                .Page(departmentSearchDTO.CurrentPage, departmentSearchDTO.PageSize)
                .ToListAsync();

            //总数据量
            var pageCount = await query.CountAsync();

            //返回数据
            var pageResult = new PageResult<DepartmentView>(departmentList, pageCount);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }

        /**
         * 新增部门
         */
        public async Task<RequestResponseModel<string>> AddDepartment(DepartmentAddDTO departmentAddDTO)
        {
            //操作数据
            DateTime date = DateTime.Now;
            Guid guid = Guid.NewGuid();

            //生成新数据
            var department = new DepartmentModel
            {
                Id = guid.ToString(),
                Code = departmentAddDTO.Code,
                Name = departmentAddDTO.Name,
                CreateTime = date
            };

            //新增部门
            _context.Department.Add(department);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "新增成功", guid.ToString());
        }

        /**
         * 更新部门
         */
        public async Task<RequestResponseModel<string>> PutDepartment(DepartmentUpdateDTO departmentUpdateDTO)
        {
            //查找数据
            var query = await _context.Department.FindAsync(departmentUpdateDTO.Id);

            //校验数据
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", departmentUpdateDTO.Id);
            };

            //生成新数据
            query.Name = departmentUpdateDTO.Name;

            //更新部门表
            _context.Department.Update(query);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "更新成功", departmentUpdateDTO.Id);
        }

        /**
         * 删除部门
         */
        public async Task<RequestResponseModel<string>> DeleteDepartment(string Id)
        {
            //判空
            if (string.IsNullOrEmpty(Id))
            {
                return RequestResponse.CreateResponse(400, "Id为空", Id);
            };

            //校验Id有效性
            var query = await _context.Department.FindAsync(Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            };

            //删除操作
            _context.Department.Remove(query);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "删除成功", Id);
        }
    }
}
